﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace _1Dot9MasterTransactions
{
    [TransactionalContext]
    public abstract class ComponentBase : ContextBoundObject, IDisposable
    {
        // Fields
        private TransactionScope _Scope;
        private TransactionInfo _TransactionInfo;

        // Methods
        protected ComponentBase()
        {
        }

        protected virtual void Activate()
        {
        }

        protected virtual void Deactivate()
        {
        }

        public void Dispose()
        {
        }

        internal void DoActivate()
        {
            this.Activate();
        }

        internal void DoDeactivate()
        {
            this.Deactivate();
        }

        protected void SetAbort()
        {
            if (this._TransactionInfo != null)
            {
                if (!this._TransactionInfo.IsManualComplete)
                {
                    throw new InvalidOperationException("ManualComplete attribute is not specified.");
                }
                TransactionController.SetAbort();
            }
        }

        protected void SetComplete()
        {
            if (this._TransactionInfo != null)
            {
                if (!this._TransactionInfo.IsManualComplete)
                {
                    throw new InvalidOperationException("ManualComplete attribute is not specified.");
                }
                TransactionController.SetComplete(this._TransactionInfo, this._Scope);
            }
        }

        // Properties
        internal TransactionScope Scope
        {
            get
            {
                return this._Scope;
            }
            set
            {
                this._Scope = value;
            }
        }

        internal TransactionInfo TransactionInfo
        {
            get
            {
                return this._TransactionInfo;
            }
            set
            {
                this._TransactionInfo = value;
            }
        }
    }
}
